/******************************************************************************
Paper: The Impact of Welfare on Intergroup Relations
Author: Akshay Dixit

CPHS: This .do file produces Figure S9 and Table S20 (Dynamic treatment effects: Bordering districts in neighboring states)
******************************************************************************/

clear all
set scheme white_tableau

gl data_cmie "$identity/data/cphs"
cd "$analysis"

******************************************************************************

*** Effect of Rythu Bandhu: Bordering districts in neighboring states ***

u "$data_cmie/merged_data_clean.dta", clear

* Keep data only for districts that share a border with Telangana, but are in another state
keep if district == "Raichur" | district == "Yadgiri" | district == "Gulbarga" | district == "Bidar" | district == "Nanded" | district == "Yavatmal" | district == "Chandrapur" | district == "East Godavari" | district == "West Godavari" | district == "Krishna" | district == "Guntur" | district == "Prakasam" | district == "Kurnool" 

* Destring variables on borrowing
foreach var in borrowed_consumption_expendi_15 borrowed_consumption_expendi_16 {
	replace `var' = "" if `var' == "DK"
	destring `var', replace
}

* Keep relevant variables
keep hh_id state district telangana region_type district_id farmer laborer has_outstanding_borrowing_* borrowed_rel_friends_* borrowed_consumption_expendi_* borrowed_bank_* borrowed_shops_1*

* Reshape data from household to household-wave
reshape long has_outstanding_borrowing_ borrowed_rel_friends_ borrowed_rel_friends_co_ borrowed_consumption_expendi_ borrowed_shops_, i(hh_id) j(wave)
ren *_ *

keep if farmer == 1 | laborer == 1
keep if wave >= 11 & wave <= 16

tab has_outstanding_borrowing
tab borrowed_rel_friends
tab borrowed_shops

* Define district * wave for clustering standard errors
g district_wave = (district_id * 100) + wave
codebook district_wave

* Evaluate dynamic effects with wave 13 (Jan-Apr 2018) as base wave
levelsof wave, local(levels)
foreach l of local levels {
	g wave_`l' = (wave == `l')
	g farmer_wave_`l' = farmer * wave_`l'
}

ren farmer_wave_11 farmer_may_aug2017
ren farmer_wave_12 farmer_sep_dec2017
ren farmer_wave_13 farmer_jan_apr2018
ren farmer_wave_14 farmer_may_aug2018
ren farmer_wave_15 farmer_sep_dec2018
ren farmer_wave_16 farmer_jan_apr2019

lab var farmer_may_aug2017 "Treated * May-Aug 2017"
lab var farmer_sep_dec2017 "Treated * Sep-Dec 2017"
lab var farmer_jan_apr2018 "Treated * Jan-Apr 2018"
lab var farmer_may_aug2018 "Treated * May-Aug 2018"
lab var farmer_sep_dec2018 "Treated * Sep-Dec 2018"
lab var farmer_jan_apr2019 "Treated * Jan-Apr 2019"

gl wave_farmer = "farmer_may_aug2017 farmer_sep_dec2017 farmer_may_aug2018 farmer_sep_dec2018 farmer_jan_apr2019"
gl wave = "wave_11 wave_12 wave_14 wave_15 wave_16"

areg borrowed_rel_friends $wave_farmer i.wave farmer, absorb(hh_id) vce(cluster district_wave)
eststo e_rel_friends
coefplot e_rel_friends, keep(farmer_*) rename(farmer_*="") xlabel(, angle(45)) vert xline(3) yline(0) ylabel(-0.4(0.1)0.4) ytitle("Estimated coefficient")
graph export "borrowings_trend_borderingdistricts.png", as(png) replace

outreg2 using "borrowings_trend_borderingdistricts.tex", keep(farmer_*) append label tex nor2 nocons
cap erase "borrowings_trend_borderingdistricts.txt"

******************************************************************************

clear
